package org.xhy.beanchameleon.sample;

import org.xhy.beanchameleon.annotation.ChameleonX;
import org.xhy.beanchameleon.compiler.MemoryCompiler;
import org.xhy.beanchameleon.core.Chameleon;
import org.xhy.beanchameleon.core.ChameleonHandler;
import org.xhy.beanchameleon.core.ChameleonSrcCode;
import org.xhy.beanchameleon.service.MemoryChameleonManager;
import org.xhy.beanchameleon.loader.DynamicClassLoader;
import org.xhy.beanchameleon.pipeline.ChainContext;
import org.xhy.beanchameleon.pipeline.ChainController;
import org.xhy.beanchameleon.pipeline.ChainProcess;
import org.xhy.beanchameleon.validator.ChameleonValidator;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-03-26 22:44
 */
public class Sample {
    static class ByteClassLoader  extends ClassLoader {
        private final byte[] bytes;

        public ByteClassLoader(byte[] bytes) {
            this.bytes = bytes;
        }
        @Override
        public Class<?> findClass(String name) {
            return defineClass(null, bytes, 0, bytes.length);
        }
    }
    // 创建 java src
    static String javaSrc = "package org.xhy.beanchameleon;\n" +
            "\n" +
            "import org.xhy.beanchameleon.annotation.ChameleonX;\n" +
            "import org.xhy.beanchameleon.core.Chameleon;\n" +
            "\n" +
            "@ChameleonX(\"test\")\n" +
            "public class Test implements Chameleon<String> {\n" +
            "    @Override\n" +
            "    public void process(String o) {\n" +
            " System.out.println(o);"+
            "\n" +
            "    }\n" +
            "}\n";

    public static void main(String[] args) {
        exec();
    }
    public static void exec(){

        final ChainController chainController = new ChainController();
        final ChainContext chainContext = new ChainContext();
        chainContext.setProcessModel(new ChameleonSrcCode(javaSrc));
        final ChameleonValidator validator = new ChameleonValidator();
        final DynamicClassLoader loader = new DynamicClassLoader();
        final MemoryCompiler memoryCompiler = new MemoryCompiler();
        final MemoryChameleonManager manager = new MemoryChameleonManager();
        final List<ChainProcess> list = Arrays.asList(validator,memoryCompiler, loader, manager);
        chainController.addChain("test", list);
        chainController.process("test",chainContext);
        if (chainContext.getNeedBreak()) {
            System.out.println(chainContext.getException());
            return;
        }
        final ChameleonHandler<Object> handler = new ChameleonHandler<>(manager);
        String name = "test";
        // 执行
        handler.exec(name,"xhy hi"); // aaaa
        chainController.process("test",chainContext);
        if (chainContext.getNeedBreak()) {
            System.out.println(chainContext.getException());
            return;
        }
        handler.exec(name,"小胡鸭 hi"); // 重新载入

        manager.remove("test"); // 删除

        handler.exec(name,"小胡鸭 hi");

    }


}


